package com.LinSoft.EquationSolver;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.util.Log;
import java.math.*;
import javax.xml.transform.sax.*;

public class x2solve extends Activity
{
	protected EditText ce_a, ce_b, ce_c;
	protected TextView ct_res;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		Log.d("x2", "init");
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.l_x2solve);
		
		Log.d("x2", "layout");
		
		ce_a = (EditText)findViewById(R.id.xe_x2_a);
		ce_b = (EditText)findViewById(R.id.xe_x2_b);
		ce_c = (EditText)findViewById(R.id.xe_x2_c);
		ct_res = (TextView)findViewById(R.id.xt_x2_res);
		Log.d("x2", "read ok");
		Button cb_solve = (Button)findViewById(R.id.xb_x2_solve);
		cb_solve.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					solve();
				}
			});
		
		ce_a.setOnKeyListener(new OnKeyListener()
			{
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					if(keyCode == KeyEvent.KEYCODE_ENTER
					   && event.getAction() == KeyEvent.ACTION_DOWN)
					{
						ce_b.requestFocus();
						return true;
					}
					return false;
				}
			});
		ce_b.setOnKeyListener(new OnKeyListener()
			{
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					if(keyCode == KeyEvent.KEYCODE_ENTER
					   && event.getAction() == KeyEvent.ACTION_DOWN)
					{
						ce_c.requestFocus();
						return true;
					}
					return false;
				}
			});
		ce_c.setOnKeyListener(new OnKeyListener()
			{
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					if(keyCode == KeyEvent.KEYCODE_ENTER
					   && event.getAction() == KeyEvent.ACTION_DOWN)
					{
						solve();
						return true;
					}
					return false;
				}
			});
			
		Log.d("x2", "implement ok");
	}
	
	protected void solve()
	{
		int a, b, c, d, t;
		String s = getResources().getString(R.string.err_x2_noback);
		boolean neg = false;
		a = Integer.parseInt(ce_a.getText().toString());
		b = Integer.parseInt(ce_b.getText().toString());
		c = Integer.parseInt(ce_c.getText().toString());
		d = b * b - 4 * a * c;
		if(d < 0)
			ct_res.setText(R.string.err_x2_nosol);
		else if(d == 0)
		{
			if((-b) * a < 0)
				neg = true;
			t = gcd(b = Math.abs(b), a = 2 * Math.abs(a));
			b /= t;
			a /= t;
			s = "X1 = X2 = ";
			if(neg)
				s += "-";
			if(a == 1)
				s += b;
			else
				s += "(" + b + ")/(" + a + ")";
			ct_res.setText(s);
		}
		else
		{
			int td = (int)Math.sqrt((double)d) + 1, temp = 1;
			
			for(int i = td; i >=2; --i)
			{
				if(d % (i * i) == 0)
				{
					d /= i * i;
					temp = i;
					break;
				}
			}
			
			if(d == 1)
			{
				int ta, tb;
				if(a * (temp - b) < 0)
					neg = true;
				t = gcd(ta = (2 * Math.abs(a)), tb = Math.abs(temp - b));
				ta /= t;
				tb /= t;
				s = "X1 = ";
				if(neg)
					s += "-";
				if(ta == 1)
					s += tb;
				else
					s += "(" + tb + ")/(" + ta + ")";
				
				s += "\nX2 = ";
				
				neg = false;
				if(a * (-temp - b) < 0)
					neg = true;
				t = gcd(ta = (2 * Math.abs(a)), tb = Math.abs(-temp - b));
				ta /= t;
				tb /= t;
				if(neg)
					s += "-";
				if(ta == 1)
					s += tb;
				else
					s += "(" + tb + ")/(" + ta + ")";
			}
			else
			{
				if(a < 0)
					b = -b;
				
				s = "X1 = ";
				s += "( " + (-b) + "+";
				if(temp != 1)
					s += temp;
				s += "√(" + d + ") )/(" + Math.abs(2 * a) + ")";
				
				s += "\nX2 = ";
				s += "( " + (-b) + "-";
				if(temp != 1)
					s += temp;
				s += "√(" + d + ") )/(" + Math.abs(2 * a) + ")";
				
			}
			
			ct_res.setText(s);
		}
		ce_a.requestFocus();
	}
	
	protected int gcd(int a, int b)
	{
		return (b == 0 ? a : gcd(b, a % b));
	}
}
